home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_2.5 / rgb2gray / rgb2gray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  5.0 KB  |  162 lines

  1. /* 
  2.  * rgb2gray.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* RGB2GRAY:    program converts 24-bit true color image to
  10.  *                            grayscale image.  Type of grayscale depends
  11.  *                              on -t option.
  12.  *                              usage: rgb2gray inFile outFile -t type
  13.  *                              The <type> argument defines what type
  14.  *              of grayscale image to output
  15.  *                              r|R = red plane
  16.  *                              g|G = green plane
  17.  *                              b|B = blue plane
  18.  *                              i|I = intensity
  19.  *                              h|H = hue
  20.  *                              s|S = saturation
  21.  *                              
  22.  */
  23.  
  24. #include "rgb2gray.h"
  25. unsigned char **image;          /* input/output image */
  26. unsigned char **imagetmp;       /* used for swapping RGB planes */
  27. extern short tiffInput;         /* flag=0 if no ImageIn to set tags; else =1 */
  28.  
  29. void
  30. main (int argc, char *argv[])
  31. {
  32.   Image *imgIO;                 /* structure for I/O images */
  33.   char type;                    /* type of image to output */
  34.   int ix, iy;
  35.   double r, g, b, intens, min, c, d, temp;
  36.  
  37.   if (argc < 4)
  38.     usage (argv[0]);
  39.  
  40.   /* read the image in */
  41.   imgIO = ImageIn (argv[1]);
  42.  
  43.   /* check to make sure it's an RGB image!! */
  44.   if (imgIO->bps != 8 || imgIO->spp != 3) {
  45.     printf ("Input file is not an RGB image!!!\n");
  46.     exit (1);
  47.   }
  48.   /* reset tiffInput so that we write a grayscale file (i.e tags are not copied) */
  49.   tiffInput = 0;
  50.  
  51.   type = argv[3][0];
  52.   switch (type) {
  53.   case 'r':
  54.   case 'R':
  55.     imagetmp = imgIO->img;
  56.     imgIO->img = imgIO->imgR;
  57.     imgIO->imgR = imagetmp;
  58.     break;
  59.   case 'g':
  60.   case 'G':
  61.     imagetmp = imgIO->img;
  62.     imgIO->img = imgIO->imgG;
  63.     imgIO->imgG = imagetmp;
  64.     break;
  65.   case 'b':
  66.   case 'B':
  67.     imagetmp = imgIO->img;
  68.     imgIO->img = imgIO->imgB;
  69.     imgIO->imgB = imagetmp;
  70.     break;
  71.   case 'i':
  72.   case 'I':
  73.     for (ix = 0; ix < imgIO->width; ix++) {
  74.       for (iy = 0; iy < imgIO->height; iy++) {
  75.         r = (double) imgIO->imgR[iy][ix] / 255.0;
  76.         g = (double) imgIO->imgG[iy][ix] / 255.0;
  77.         b = (double) imgIO->imgB[iy][ix] / 255.0;
  78.         intens = (r + g + b) / 3.0;
  79.         imgIO->img[iy][ix] = (unsigned char) (255.0 * intens + 0.5);
  80.       }
  81.     }
  82.     break;
  83.   case 'h':
  84.   case 'H':
  85.     for (ix = 0; ix < imgIO->width; ix++) {
  86.       for (iy = 0; iy < imgIO->height; iy++) {
  87.         r = (double) imgIO->imgR[iy][ix] / 255.0;
  88.         g = (double) imgIO->imgG[iy][ix] / 255.0;
  89.         b = (double) imgIO->imgB[iy][ix] / 255.0;
  90.         c = 0.5 * (2.0 * r - g - b);
  91.         d = sqrt ((r - g) * (r - g) + (r - b) * (g - b));
  92.         if (d == 0.0)
  93.           imgIO->img[iy][ix] = (unsigned char) 255;  /* arbitrary value -> hue undefined */
  94.         else {
  95.           temp = c / d;         /* imprecision causes > |1| */
  96.           if (temp > 1.0)
  97.             temp = 1.0;
  98.           else if (temp < -1.0)
  99.             temp = -1.0;
  100.           temp = acos (temp);
  101.           if (b > g)
  102.             temp = 2.0 * M_PI - temp;
  103.           imgIO->img[iy][ix] = (unsigned char) (temp * 100.0 / M_PI);  /* scale 0-200 */
  104.         }
  105.       }
  106.     }
  107.     break;
  108.   case 's':
  109.   case 'S':
  110.     for (ix = 0; ix < imgIO->width; ix++) {
  111.       for (iy = 0; iy < imgIO->height; iy++) {
  112.         r = (double) imgIO->imgR[iy][ix] / 255.0;
  113.         g = (double) imgIO->imgG[iy][ix] / 255.0;
  114.         b = (double) imgIO->imgB[iy][ix] / 255.0;
  115.         intens = (r + g + b) / 3.0;
  116.         min = (r < g) ? r : g;
  117.         min = (min < b) ? min : b;
  118.         if (intens == 0.0)
  119.           imgIO->img[iy][ix] = (unsigned char) 0;
  120.         else
  121.           imgIO->img[iy][ix] = (unsigned char) (255.0 * (1.0 - min / intens) + 0.5);
  122.       }
  123.     }
  124.     break;
  125.   default:
  126.     printf ("Illegal option %c\n", type);
  127.     exit (1);
  128.   }
  129.   /* write the image out */
  130.   ImageOut (argv[2], imgIO);
  131.   exit (0);
  132. }
  133.  
  134. /*
  135.  * usage of routine
  136.  */
  137.  
  138. void
  139. usage (char *progname)
  140. {
  141.   progname = last_bs (progname);
  142.   printf ("USAGE: %s inimg outimg type [-L]\n", progname);
  143.   printf ("\n%s accepts RGB color image input file, and produces\n", progname);
  144.   printf ("a grayscale file specified by type.\n\n");
  145.   printf ("ARGUMENTS:\n");
  146.   printf ("    inimg: input image filename (TIF)\n");
  147.   printf ("   outimg: output image filename (TIF)\n");
  148.   printf ("     type: r|R produces RED plane\n");
  149.   printf ("           g|G produces GREEN plane\n");
  150.   printf ("           b|B produces BLUE plane\n");
  151.   printf ("           i|I produces INTENSITY values\n");
  152.   printf ("           h|H produces HUE values\n");
  153.   printf ("           s|S produces SATURATION values\n");
  154.   printf ("           NOTE: the HUE values are arbitrarily scaled from\n");
  155.   printf ("                 0 to 200, where red=0, green=67, and blue=133.\n");
  156.   printf ("                 The gray tvalue, i.e. red=green=blue,\n");
  157.   printf ("                 is arbitrarily set to 255.\n\n");
  158.   printf ("OPTIONS:\n");
  159.   printf ("       -L: print Software License for this module\n");
  160.   exit (1);
  161. }
  162.